/*
 * Copyright (C) 2010 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.fanli.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.common.network.ApiAsyncTask;
import com.mol.market.common.network.ApiAsyncTask.ApiRequestListener;
import com.mol.market.common.network.MarketAPI;
import com.mol.market.common.util.Utils;
import com.mol.market.common.widget.BaseActivity;
import com.mol.market.common.widget.LoadingDrawable;
import com.mol.market.fanli.ui.TaskInformationActivity.RebateTask;


/**
 * 产品详细页预加载页面
 * 
 * @author andrew
 * @date 2011-4-19
 * 
 */
public class FanliPreloadActivity extends BaseActivity implements
    ApiRequestListener {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.pre_loading);

    final Intent intent = getIntent();

    ProgressBar mProgress = (ProgressBar) findViewById(R.id.progressbar);
    LoadingDrawable loading = new LoadingDrawable(getApplicationContext(),
        LoadingDrawable.SIZE_SMALL, R.color.hot3, R.color.color_e, 200);
    mProgress.setIndeterminateDrawable(loading);
    mProgress.setVisibility(View.VISIBLE);

    // 通过返利任务ID来获取内容
    String pId = intent.getStringExtra(Constants.EXTRA_PRODUCT_ID);
    MarketAPI.getExchangeTaskDetail(getApplicationContext(), this, pId);
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.app.Activity#onResume()
   */
  @Override
  protected void onResume() {
    super.onResume();

  }

  /*
   * (non-Javadoc)
   * 
   * @see android.app.Activity#onPause()
   */
  @Override
  protected void onPause() {
    super.onPause();
  }

  @Override
  public void onSuccess(int method, Object obj) {

    if (MarketAPI.ACTION_GET_EXCHANGE_TASK_DETAIL == method) {

      RebateTask task = (RebateTask) obj;

      Intent intent = new Intent(getApplicationContext(),
          TaskInformationActivity.class);
      intent.putExtra(Constants.EXTRA_PRDUCT_DETAIL, task);

      finish();
      startActivity(intent);
    }
  }

  @Override
  public void onError(int method, int statusCode) {

    if (ApiAsyncTask.TIMEOUT_ERROR == statusCode) {
      // 网络异常
      Utils.makeEventToast(getApplicationContext(),
          getString(R.string.no_network), false);
    } else {
      Utils.makeEventToast(getApplicationContext(),
          getString(R.string.label_not_found), false);
    }
    finish();
  }
}